package com.neu.cpsp.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neu.cpsp.dao.RegulationTypeMapper;
import com.neu.cpsp.pojo.RegulationType;
import com.neu.cpsp.pojo.RegulationTypeAndDetail;
import com.neu.cpsp.service.RegulationTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author neu.xiaojiacheng
 * @version 1.0
 * @date 2022/2/24 15:02
 */
@Service
@Transactional
public class RegulationTypeServiceImpl implements RegulationTypeService {
    @Autowired
    private RegulationTypeMapper regulationTypeMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean submitAllInfo(RegulationType regulationType) {
        int flag = regulationTypeMapper.insert(regulationType);
        if (flag > 0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<RegulationType> list = regulationTypeMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByName(int pageSize, int currentPage, String name) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<RegulationType> list = regulationTypeMapper.selectByName(name);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return regulationTypeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(RegulationType record) {
        return regulationTypeMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public List<RegulationTypeAndDetail> getTypeAndDetail(RegulationType record) {
        return regulationTypeMapper.selectTypeAndDetail(record);
    }
}
